package repository

import (
    "gorm.io/gorm"
    "{{.ProjectModule}}/models"
)

type {{.StructName}}Repository struct {
    DB *gorm.DB
}

func New{{.StructName}}Repository(db *gorm.DB) *{{.StructName}}Repository {
    return &{{.StructName}}Repository{DB: db}
}

func (r *{{.StructName}}Repository) Create{{.StructName}}({{.VarName}} *models.{{.StructName}}) error {
    return r.DB.Create({{.VarName}}).Error
}

func (r *{{.StructName}}Repository) GetAll{{.StructName}}s() ([]models.{{.StructName}}, error) {
    var {{.VarName}}s []models.{{.StructName}}
    err := r.DB.Find(&{{.VarName}}s).Error
    return {{.VarName}}s, err
}

func (r *{{.StructName}}Repository) Get{{.StructName}}ByID(id uint) (*models.{{.StructName}}, error) {
    var {{.VarName}} models.{{.StructName}}
    err := r.DB.First(&{{.VarName}}, id).Error
    if err != nil {
        return nil, err
    }
    return &{{.VarName}}, nil
}

func (r *{{.StructName}}Repository) Update{{.StructName}}({{.VarName}} *models.{{.StructName}}) error {
    return r.DB.Save({{.VarName}}).Error
}

func (r *{{.StructName}}Repository) Delete{{.StructName}}(id uint) error {
    return r.DB.Delete(&models.{{.StructName}}{}, id).Error
}